﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.OracleClient;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;

namespace Logistics.DataAccess
{
    public class TC_OWS_FILE
    {
        public bool Add(Model.TC_OWS_FILE model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO TC_OWS_FILE(");
            strSql.Append("TC_OWS000,");
            strSql.Append("TC_OWS001,");
            strSql.Append("TC_OWS002,");
            strSql.Append("TC_OWS003,");
            strSql.Append("TC_OWS004,");
            strSql.Append("TC_OWS005,");
            strSql.Append("TC_OWS006,");
            strSql.Append("TC_OWS007,");
            strSql.Append("TC_OWS008,");
            strSql.Append("TC_OWS009,");
            strSql.Append("TC_OWS010,");
            strSql.Append("TC_OWS011,");
            strSql.Append("TC_OWS012,");
            strSql.Append("TC_OWS013,");
            strSql.Append("TC_OWS014,");
            strSql.Append("TC_OWS019,");
            strSql.Append("TC_OWS022)");
            strSql.Append(" VALUES (");
            strSql.Append("@in_TC_OWS000,");
            strSql.Append("@in_TC_OWS001,");
            strSql.Append("@in_TC_OWS002,");
            strSql.Append("@in_TC_OWS003,");
            strSql.Append("@in_TC_OWS004,");
            strSql.Append("@in_TC_OWS005,");
            strSql.Append("@in_TC_OWS006,");
            strSql.Append("@in_TC_OWS007,");
            strSql.Append("@in_TC_OWS008,");
            strSql.Append("@in_TC_OWS009,");
            strSql.Append("@in_TC_OWS010,");
            strSql.Append("@in_TC_OWS011,");
            strSql.Append("@in_TC_OWS012,");
            strSql.Append("@in_TC_OWS013,");
            strSql.Append("@in_TC_OWS014,");
            strSql.Append("@in_TC_OWS019,");
            strSql.Append("@in_TC_OWS022)");
            OracleParameter[] cmdParms = {
                DBHelper.CreateDbParameter("@in_TC_OWS000", DbType.String,20 ,DBValue.GetDBValue(model.TC_OWS000)),
                DBHelper.CreateDbParameter("@in_TC_OWS001", DbType.String,20 ,DBValue.GetDBValue(model.TC_OWS001)),
                DBHelper.CreateDbParameter("@in_TC_OWS002", DbType.String,20 ,DBValue.GetDBValue(model.TC_OWS002)),
                DBHelper.CreateDbParameter("@in_TC_OWS003", DbType.String,20 ,DBValue.GetDBValue(model.TC_OWS003)),
                DBHelper.CreateDbParameter("@in_TC_OWS004", DbType.String,20 ,DBValue.GetDBValue(model.TC_OWS004)),
                DBHelper.CreateDbParameter("@in_TC_OWS005", DbType.String,20 ,DBValue.GetDBValue(model.TC_OWS005)),
                DBHelper.CreateDbParameter("@in_TC_OWS006", DbType.String,20 ,DBValue.GetDBValue(model.TC_OWS006)),
                DBHelper.CreateDbParameter("@in_TC_OWS007", DbType.Decimal,20,DBValue.GetDBValue(model.TC_OWS007)),
                DBHelper.CreateDbParameter("@in_TC_OWS008", DbType.Decimal,20 ,DBValue.GetDBValue(model.TC_OWS008)),
                DBHelper.CreateDbParameter("@in_TC_OWS009", DbType.Decimal,20 ,DBValue.GetDBValue(model.TC_OWS009)),
                DBHelper.CreateDbParameter("@in_TC_OWS010", DbType.Decimal,20 ,DBValue.GetDBValue(model.TC_OWS010)),
                DBHelper.CreateDbParameter("@in_TC_OWS011", DbType.Decimal,20 ,DBValue.GetDBValue(model.TC_OWS011)),
                DBHelper.CreateDbParameter("@in_TC_OWS012", DbType.String,50 ,DBValue.GetDBValue(model.TC_OWS012)),
                DBHelper.CreateDbParameter("@in_TC_OWS013", DbType.String,50 ,DBValue.GetDBValue(model.TC_OWS013)),
                DBHelper.CreateDbParameter("@in_TC_OWS014", DbType.Decimal,20,DBValue.GetDBValue(model.TC_OWS014)),
                DBHelper.CreateDbParameter("@in_TC_OWS019", DbType.String,20 ,DBValue.GetDBValue(model.TC_OWS019)),
                DBHelper.CreateDbParameter("@in_TC_OWS022", DbType.String,50,DBValue.GetDBValue(model.TC_OWS022)),
            };

            return DBHelper.ExecuteSql(strSql.ToString(), cmdParms) > 0;
        }

        public List<Model.TC_OWS_FILE> GetList(List<string> owi, string l_date, string status)
        {
            StringBuilder strSql = new StringBuilder("SELECT * FROM TC_OWS_FILE WHERE 1=1");
            if (owi.Count == 1)
                strSql.AppendFormat(" AND TC_OWS009 = '{0}'", string.Join(",", owi));
            if (owi.Count > 1)
                strSql.AppendFormat(" AND TC_OWS009 IN ({0})", string.Join(",", owi));
            if (!string.IsNullOrEmpty(l_date))
                strSql.AppendFormat(" AND TC_OWS006 = to_date('{0}','yyyy-mm-dd')", l_date);
            if (!string.IsNullOrEmpty(status))
                strSql.AppendFormat(" AND TC_OWS019='{0}'", status);
            using (DbDataReader dr = DBHelper.ExecuteReader(strSql.ToString()))
            {
                List<Model.TC_OWS_FILE> lst = GetList(dr);
                return lst;
            }
        }

        public string Upd(Model.TC_OWS_FILE model, DateTime l_outdt)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat(" UPDATE TC_OWR_FILE SET tc_owr007 = to_date('{0}', 'yyyy-mm-dd hh24:mi:ss') WHERE tc_owr001 = '{1}' AND tc_owr002 = '{2}'", l_outdt, model.TC_OWS000, model.TC_OWS001);
            DBHelper.ExecuteSql(strSql.ToString());
            return "0";

        }

        /// <summary>
        /// 单据明细
        /// </summary>
        public List<Model.TC_OWS_FILE> GetDetail(string l_tc_owr001, string l_tc_owr002)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("Select tc_ows000, tc_ows001, tc_ows002, tc_ows003, tc_ows004, tc_ows005, ima02, ");
            strSql.Append("tc_ows006, tc_ows007, tc_ows008, tc_ows009, tc_ows010, tc_ows011, tc_ows012, tc_ows013, tc_ows014, ");
            strSql.Append("Tc_ows019, tc_ows022 ");
            strSql.Append("From edi_test.tc_ows_file, edi_test.ima_file ");
            strSql.Append($"Where tc_ows000 ={l_tc_owr001} ");
            strSql.Append($"and tc_ows001 ={l_tc_owr002} ");
            strSql.Append("and tc_ows005 = ima01");
            using (DbDataReader dr = DBHelper.ExecuteReader(strSql.ToString()))
            {
                var list = GetList(dr);
                return list;
            }
        }

        #region 
        /// <summary>
        /// 由一行数据得到一个实体
        /// </summary>
        private Model.TC_OWS_FILE GetModel(DbDataReader dr)
        {
            Model.TC_OWS_FILE model = new Model.TC_OWS_FILE();
            model.TC_OWS001 = DBHelper.GetString(dr["TC_OWS001"]);
            model.TC_OWS002 = DBHelper.GetString(dr["TC_OWS002"]);
            model.TC_OWS003 = DBHelper.GetString(dr["TC_OWS003"]);
            model.TC_OWS004 = DBHelper.GetString(dr["TC_OWS004"]);
            model.TC_OWS005 = DBHelper.GetString(dr["TC_OWS005"]);
            model.TC_OWS006 = DBHelper.GetString(dr["TC_OWS006"]);
            model.TC_OWS007 = DBHelper.GetString(dr["TC_OWS007"]);
            model.TC_OWS008 = DBHelper.GetString(dr["TC_OWS008"]);
            model.TC_OWS009 = DBHelper.GetString(dr["TC_OWS009"]);
            model.TC_OWS010 = DBHelper.GetString(dr["TC_OWS010"]);
            model.TC_OWS011 = DBHelper.GetString(dr["TC_OWS011"]);
            model.TC_OWS012 = DBHelper.GetString(dr["TC_OWS012"]);
            model.TC_OWS013 = DBHelper.GetString(dr["TC_OWS013"]);
            model.TC_OWS014 = DBHelper.GetString(dr["TC_OWS014"]);
            model.TC_OWS019 = DBHelper.GetDateTime(dr["TC_OWS019"]);
            model.TC_OWS022 = DBHelper.GetString(dr["TC_OWS022"]);
            try
            {
                model.ima02 = DBHelper.GetString(dr["ima02"]);
            }
            catch { }
            return model;
        }

        /// <summary>
        /// 由DbDataReader得到泛型数据列表
        /// </summary>
        private List<Model.TC_OWS_FILE> GetList(DbDataReader dr)
        {
            List<Model.TC_OWS_FILE> lst = new List<Model.TC_OWS_FILE>();
            while (dr.Read())
            {
                lst.Add(GetModel(dr));
            }
            return lst;
        }
        #endregion
    }
}
